package per.lance.easytomcat.controller;

import com.google.gson.Gson;
import per.lance.easytomcat.annotation.RequestMapping;
import per.lance.easytomcat.bean.Book;
import per.lance.easytomcat.dao.BookDao;
import per.lance.easytomcat.database.DB;
import per.lance.easytomcat.env.StatusCodes;
import per.lance.easytomcat.net.HttpRequest;
import per.lance.easytomcat.net.HttpRespone;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;

public class BookController extends Controller {

    Gson gson = new Gson();
    BookDao bookDao = new BookDao();

    @RequestMapping(value = "/books", requestMethod = RequestMapping.RequestMethod.GET)
    public void getBooksData(HttpRequest httpRequest, HttpRespone httpRespone) {
        httpRespone.setStatusCode(StatusCodes.SUCCESS);
        httpRespone.getResponeHeader().put("Content-Type", "application/json;charset=utf-8");
        httpRespone.setResponeBody(gson.toJson(bookDao.selectAll(), ArrayList.class).getBytes());
    }

    @RequestMapping(value = "/book", requestMethod = RequestMapping.RequestMethod.GET)
    public void getBookData(HttpRequest httpRequest, HttpRespone httpRespone) {

    }

    @RequestMapping(value = "/books", requestMethod = RequestMapping.RequestMethod.POST)
    public void addBook(HttpRequest httpRequest, HttpRespone httpRespone) {
        HashMap<String, Object> requestBody = httpRequest.getRequestBody();
        Book book = new Book();
        String uuid = UUID.randomUUID().toString();
        book.setId(uuid);
        book.setName(requestBody.get("bookname").toString());
        bookDao.add(book);
        httpRespone.setStatusCode(StatusCodes.SUCCESS);
        httpRespone.getResponeHeader().put("Content-Type", "text/html;charset=utf-8");
        httpRespone.setResponeBody("success".getBytes());
    }

    @RequestMapping(value = "/books", requestMethod = RequestMapping.RequestMethod.PUT)
    public void updateBook(HttpRequest httpRequest, HttpRespone httpRespone) {
        HashMap<String, Object> requestBody = httpRequest.getRequestBody();
        String id = requestBody.get("bookid").toString();
        String bookName = requestBody.get("bookname").toString();
        String bookprice = requestBody.get("bookprice").toString();
        Book book = new Book();
        book.setId(id);
        book.setName(bookName);
        book.setPrice(Float.valueOf(bookprice));
        bookDao.update(book);

        httpRespone.setStatusCode(StatusCodes.SUCCESS);
        httpRespone.getResponeHeader().put("Content-Type", "text/html;charset=utf-8");
        httpRespone.setResponeBody("success".getBytes());
    }

    @RequestMapping(value = "/books", requestMethod = RequestMapping.RequestMethod.DEL)
    public void deleteBook(HttpRequest httpRequest, HttpRespone httpRespone) {
        HashMap<String, Object> requestBody = httpRequest.getRequestBody();
        String id = requestBody.get("bookid").toString();
        if (DB.getInstance().getBooks().containsKey(id)) {
            bookDao.deleteById(id);
        }
        httpRespone.setStatusCode(StatusCodes.SUCCESS);
        httpRespone.getResponeHeader().put("Content-Type", "text/html;charset=utf-8");
        httpRespone.setResponeBody("success".getBytes());
    }

    @RequestMapping(value = "/book/\\w+", requestMethod = RequestMapping.RequestMethod.GET)
    public void getBook(HttpRequest httpRequest, HttpRespone httpRespone) {
        String[] strs = httpRequest.getUri().getPath().split("/");
        String id = strs[strs.length - 1];
        httpRespone.setStatusCode(StatusCodes.SUCCESS);
        httpRespone.getResponeHeader().put("Content-Type", "text/html;charset=utf-8");
        Book book = bookDao.getById(id);
        httpRespone.setResponeBody(gson.toJson(book, Book.class).getBytes());
    }


}
